/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.app.bench;

import java.util.LinkedList;

/**
 * 命令执行与管理，如撤销
 * @author chunquedong
 * 
 */
public class CommandManager {
	LinkedList<Command> doCommands=new LinkedList<Command>();
	LinkedList<Command> undoCommands=new LinkedList<Command>();
	
	public void execute(Command cmd){
		cmd.execute();
		doCommands.push(cmd);
	}
	
	public void undo(){
		if(doCommands.size()==0)return;
		
		Command cmd=doCommands.pop();
		if(cmd.isUndoable()){
			cmd.rollback();
		}
		undoCommands.push(cmd);
	}
	
	public void redo(){
		if(undoCommands.size()==0)return;
		
		Command cmd=undoCommands.pop();
		execute(cmd);
	}
}